package mumu.android.app;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import mumu.android.R;
import mumu.android.lib.tool.Tools;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;

/**
 * 
 * bind服务处理。 关闭activity后，服务终止
 * 
 * @author Administrator
 * 
 */
public class fileEncry extends Activity {

	public static String OriginalFile = Tools.getSDPath() + "/hsy/unzipfile/1/Advertisement/1.png";
	public static String EncryFile = Tools.getSDPath() + "/hsy/unzipfile/1/Advertisement/1_en";
	public static String descryFile = Tools.getSDPath() + "/hsy/unzipfile/1/Advertisement/1_des.png";
	public static final int XOR_CONST = 0X99; // 密钥

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fileencry);
		// encrImg();
		Bitmap bm = readBitmap(this, EncryFile);
		ImageView img_1 = (ImageView) findViewById(R.id.img_1);
		if (bm != null)
			img_1.setImageBitmap(bm);

	}

	public static void encrImg() {
		try {
			File efile = new File(EncryFile);
			if (!efile.exists()) {
				efile.createNewFile();
			} else {
				efile.delete();
			}
			File ofile = new File(OriginalFile);
			FileInputStream fis = new FileInputStream(ofile);
			FileOutputStream fos;

			fos = new FileOutputStream(efile);

			int read;
			while ((read = fis.read()) > -1) {
				fos.write(read ^ XOR_CONST);
			}
			fos.flush();
			fos.close();
			fis.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static Bitmap readBitmap(Context context, String fileName) {
		Bitmap bitmap = null;
		List<Byte> list = new ArrayList();
		try {
			File ofile = new File(fileName);
			InputStream is = new FileInputStream(ofile);
			int read;
			while ((read = is.read()) > -1) {
				read = read ^ XOR_CONST;
				list.add((byte) read);
			}

			byte[] arr = new byte[list.size()];
			int i = 0;
			for (Byte item : list) {
				arr[i++] = item;
			}
			bitmap = BitmapFactory.decodeByteArray(arr, 0, list.size());
			System.out.println(bitmap);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bitmap;
	}

}
